# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libgcrypt

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.6.1
pkgrel=3
pkgdesc="General purpose cryptographic library based on the code from GnuPG (mingw-w64)"
arch=('any')
url="http://www.gnupg.org"
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-libgpg-error")
groups=("${MINGW_PACKAGE_PREFIX}")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-libgpg-error" "${MINGW_PACKAGE_PREFIX}-ghostscript") # "transfig"
options=('staticlibs' 'strip')
source=("ftp://ftp.gnupg.org/gcrypt/libgcrypt/libgcrypt-${pkgver}.tar.bz2"
        'libgcrypt-mingw64-use-correct-datatype-for-GetProcessWorkingSet.patch'
        'libgcrypt-use-correct-def-file.patch'
        'libgcrypt-use-correct-asm-code-for-win64.patch'
        'Smarter-fig2dev-detection.all.patch')
md5sums=('a5a5060dc2f80bcac700ab0236ea47dc'
         '7bb247584a66a83677c618aec812ca9b'
         '531e089caca74b5daf130b7173c2a5c5'
         '8ea7a618d222f1301b0eaf596954bb51'
         'c29a3e3e1664229eca2198c2328af5f4')

prepare() {
  cd "${srcdir}/libgcrypt-${pkgver}"
  patch -p0 -i "$srcdir"/libgcrypt-mingw64-use-correct-datatype-for-GetProcessWorkingSet.patch
  patch -p0 -i "$srcdir"/libgcrypt-use-correct-def-file.patch
  #patch -p0 -i "$srcdir"/libgcrypt-use-correct-asm-code-for-win64.patch
  patch -p1 -i "$srcdir"/Smarter-fig2dev-detection.all.patch

  autoreconf -fi
}

build() {
  cp -r "$srcdir/libgcrypt-${pkgver}" "$srcdir/build-${CARCH}"
  cd "$srcdir/build-${CARCH}"
  [[ $CARCH == x86_64 ]] && BUILD_ASM="--disable-asm --disable-padlock-support" || BUILD_ASM=""
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared \
    --disable-static \
    $BUILD_ASM \
    --with-gpg-error-prefix=${MINGW_PREFIX}
  make
}

package() {
  cd "$srcdir/build-${CARCH}"
  make DESTDIR="$pkgdir" install -j1
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
}
